package com.mooer.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.bean.Shop;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;
import com.mooer.utils.ui.UiUtils;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-12 上午07:49:38 类说明
 */
public class BusinessInfo extends Activity implements OnClickListener {

	private static final String TAG = "BusinessInfo";

	private ImageView call, checkMap, signin, star, concern, share, icon,
			download1, download2;

	private TextView businessName, zifei, rigion, type, phone, address,
			youhui_info, youhui_tip;

	private LinearLayout couponLayout1, couponLayout2;

	private Shop businessInfo;

	private MooerApplication mooerApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_info);
		mooerApp = (MooerApplication) getApplication();
		Intent intent = getIntent();
		businessInfo = (Shop) intent.getSerializableExtra("businessInfo");
		couponLayout1 = (LinearLayout) findViewById(R.id.youhuixinxi);
		couponLayout2 = (LinearLayout) findViewById(R.id.youhuiquan);
		icon = (ImageView) findViewById(R.id.business_icon);
		call = (ImageView) findViewById(R.id.call);
		checkMap = (ImageView) findViewById(R.id.check_map);
		signin = (ImageView) findViewById(R.id.signin);
		star = (ImageView) findViewById(R.id.star);
		concern = (ImageView) findViewById(R.id.concern);
		share = (ImageView) findViewById(R.id.share);
		download1 = (ImageView) findViewById(R.id.download1);
		download2 = (ImageView) findViewById(R.id.download2);
		businessName = (TextView) findViewById(R.id.business_name);
		zifei = (TextView) findViewById(R.id.zifei);
		type = (TextView) findViewById(R.id.type);
		rigion = (TextView) findViewById(R.id.rigion);
		phone = (TextView) findViewById(R.id.phone);
		address = (TextView) findViewById(R.id.address);
		youhui_info = (TextView) findViewById(R.id.youhui_info);
		youhui_tip = (TextView) findViewById(R.id.youhui_tip);

		share.setVisibility(View.GONE);
		businessName.setText(businessInfo.getName());
		zifei.setText(getString(R.string.zifei_avg,
				businessInfo.getAvgConsumption()));
		type.setText(businessInfo.getCategoryName());
		rigion.setText(businessInfo.getAddress().getAreaName());
		phone.setText("电话：" + businessInfo.getPhone());
		address.setText("地址：" + businessInfo.getAddress().getAddress1());
		icon.setBackgroundResource(R.drawable.m_seller_pic_d);
		star.setBackgroundDrawable(UiUtils.selectStar(getResources(),
				businessInfo.getRating()));
		if (businessInfo.isHasCoupon()) {
			couponLayout2.setVisibility(View.VISIBLE);
		} else {
			couponLayout1.setVisibility(View.VISIBLE);
		}

		call.setOnClickListener(this);
		checkMap.setOnClickListener(this);
		signin.setOnClickListener(this);
		concern.setOnClickListener(this);
		share.setOnClickListener(this);
		download1.setOnClickListener(this);
		download2.setOnClickListener(this);

		mooerApp.getDbHelper().save(businessInfo);
		new BusinessTask().execute(businessInfo.getId());

	}

	@Override
	public void onClick(View v) {
		if (v == signin) {
			Business.instance.switchTab("signin");
		} else if (v == checkMap) {
			checkMap(businessInfo.getAddress().getLatitude(), businessInfo
					.getAddress().getLongitude());
		} else if (v == call) {
			if (businessInfo.getPhone() == null
					|| businessInfo.getPhone().length() == 0) {
				UiUtils.showToast(this, "该商家暂未提供电话");
			} else {
				call(businessInfo.getPhone());
			}
		} else if (v == concern) {
			if (mooerApp.isLoggedin()) {
				concern.setBackgroundResource(R.drawable.b_seller_attention_in);
				concern.setClickable(false);
				new ConcernTask().execute(mooerApp.getUser().getId(),
						businessInfo.getId());
			} else {
				Intent intent = new Intent(this, MoreAccountSetting.class);
				intent.putExtra("isFinish", true);
				startActivityForResult(intent, 0);
			}
		}

	}

	private void call(String phone) {
		if (phone != null && !"".equals(phone.trim())) {
			Uri uri = Uri.parse("tel:" + phone);
			Intent it = new Intent(Intent.ACTION_DIAL, uri);
			startActivity(it);
		}
	}

	private void checkMap(double latitude, double longitude) {
		if (latitude != 0 && longitude != 0) {
			try {
				String mapUri = "geo:" + latitude + "," + longitude;
				Log.i(TAG, mapUri);
				Uri uri = Uri.parse(mapUri);
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(it);

			} catch (Exception e) {
				Log.v(TAG, e.getMessage());
				Uri uri = Uri
						.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q="
								+ latitude + "," + longitude);
				try {
					Intent it = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(it);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	private class BusinessTask extends AsyncTask<Integer, String, Shop> {

		@Override
		protected Shop doInBackground(Integer... params) {
			try {
				Shop shop = HttpUtils.fetchShopDetail(params[0]);
				shop.setAvgConsumption(businessInfo.getAvgConsumption());
				shop.setRating(businessInfo.getRating());
				return shop;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Shop shop) {
			super.onPostExecute(shop);
			if (shop != null) {
				BusinessInfo.this.businessInfo = shop;
				icon.setBackgroundResource(R.drawable.m_seller_pic_d);
				star.setBackgroundDrawable(UiUtils.selectStar(getResources(),
						businessInfo.getRating()));
				if (businessInfo.isHasCoupon()) {
					couponLayout2.setVisibility(View.VISIBLE);
				}
				if (businessInfo.getPromotionId() > 0) {
					couponLayout1.setVisibility(View.VISIBLE);
					youhui_tip.setText(businessInfo.getPromotionTitle());
					youhui_info.setText(Html.fromHtml(businessInfo
							.getPromotionContent()));
				}

			}
		}

	}

	private class ConcernTask extends AsyncTask<Integer, Integer, String> {

		private boolean isException;

		@Override
		protected String doInBackground(Integer... arg0) {
			try {
				HttpUtils.doFollowShop(arg0[0], arg0[1]);
			} catch (Exception e) {
				isException = true;
				Log.e(TAG, "", e);
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (isException) {
				concern.setClickable(true);
				concern.setBackgroundResource(R.drawable.b_seller_attention_selector);
				UiUtils.showToast(BusinessInfo.this, "关注失败");
			} else {
				UiUtils.showToast(BusinessInfo.this, "关注成功");
			}
		}

	}

}
